var MongoClient = require('mongodb').MongoClient;
var async = require('async');

var driver = require('./db');
var utils = require('./utils');
var tokenDao = require('../api/tokenDao');
const Url = "mongodb://localhost:27017/manhua";

const table = 'user';


var userDao = {};

// 用户注册
userDao.reg = function (param, res) {

    var _query = {};

    if (param.username) {
        _query.username = param.username;

    }

    var data = param;

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query);
        },
        // 插入
        function (db, res, callback) {

            if (res.data.length == 0) {
                driver.insert(db, callback, table, param);
            } else {
                callback({ success: false, msg: '该用户名已经注册' }, db, res);
            }


        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }
            res.json(result);
        }
    );
};

// 用户登录 token
userDao.login = function (param, res) {
    console.log('api/login');
    var _query = {
        username: param.username,
        password: param.password
    };
    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        // 超找
        function (db, callback) {

            driver.find(db, callback, table, _query, { "password": 0, "favors": 0, "historys": 0 });
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                console.log('happen error');
                res.json(err);
                return;
            }

            if (result.success && result.data.length > 0) {

                var _user = result.data[0];


                let myToken = tokenDao.sign(_user);

                console.log(myToken);

                res.cookie('token', myToken);

                res.append('token', myToken);

            }

            res.json(result);


        }
    );
};


// 用户注销

// 获取用户信息
userDao.getUserInfo = function (userId, res) {
    var _query = {
        "_id": require('mongodb').ObjectID(userId)
    };
    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, Url);
        },
        function (db, callback) {
            driver.find(db, callback, table, _query, { "password": 0 });
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }],
        // result
        function (err, result) {
            console.log("resulte");

            if (err) {
                res.json(err);
                return;
            }

            res.json(result);
        }
    );
};


module.exports = userDao;